#include "stdinc.h"
#include "../File.h"

CFile* CFile::Create(char* path, int flags)
{
	char *s = NULL;

	if (flags & CFileSys::READ)
		s = "rb";

	if (flags & CFileSys::WRITE)
		s = "wb";

	if (!s)
		return NULL;

	FILE* f = fopen(path, s);

	if (!f)
		return NULL;

	CFile* file = new CFile;
	file->m_file = f;

	fseek(f, 0, SEEK_END);
	file->m_size = ftell(f);
	fseek(f, 0, SEEK_SET);

	return file;
}

CFile::CFile()
{
}

CFile::~CFile()
{
	fclose(m_file);
}

bool CFile::Read(void* data, int count)
{
	return fread(data, count, 1, m_file) != 0;
}

bool CFile::Write(void* data, int count)
{
	return fwrite(data, count, 1, m_file) != 0;
}

void CFile::Seek(int pos)
{
	fseek(m_file, pos, SEEK_SET);
}